package com.futvan.z.shop.sinvoice;
import com.futvan.z.framework.common.bean.Code;
import com.futvan.z.framework.common.bean.Result;
import com.futvan.z.framework.core.z;
import com.futvan.z.framework.util.FileUtil;
import com.futvan.z.framework.util.PdfUtil;
import com.futvan.z.framework.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.futvan.z.framework.core.SuperAction;
import com.futvan.z.shop.sinvoice.SinvoiceService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

@Controller
public class SinvoiceAction extends SuperAction{
	@Autowired
	private SinvoiceService sinvoiceService;

	/**
	 * 上传发票压缩包
	 * @return
	 * @throws Exception
	 */
	@CrossOrigin(origins = "*", maxAge = 360000)
	@RequestMapping(value="/invoice_zip_import")
	public @ResponseBody Result invoice_zip_import(HttpServletRequest request){
		Result result = new Result();
		List<String> uploadFileList = new ArrayList<String>();
		try {
			//将当前上下文初始化给  CommonsMutipartResolver （多部分解析器）
			CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext());
			//检查form中是否有enctype="multipart/form-data"
			if(multipartResolver.isMultipart(request)){
				//将request变成多部分request
				if(request instanceof  MultipartHttpServletRequest){
					//获取文件流
					MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
					MultipartFile file = multiRequest.getFile("fileData");
					if(file!=null){
						//请空目录
						String fileSavePath = CreateFileSavePath("invoice_temp");

						String old_filename = StringUtil.UrlDecode(file.getOriginalFilename());
						String new_filename_number = z.newNumber();
						String new_filename = new_filename_number+old_filename.substring(old_filename.lastIndexOf("."));

						//保存文件
						File save_file = new File(fileSavePath+"/"+ new_filename);
						file.transferTo(save_file);

						//解压
						FileUtil.UnZip(save_file);

						//遍历文件夹
						List<File> all_invoice_pdf = FileUtil.getPathAllFiles(save_file.getParent()+"/"+new_filename_number);
						for (File pdf : all_invoice_pdf) {
							//上传发票图片到文件服务器
							InputStream pdf_in = new FileInputStream(pdf);
							String pdf_file_url = FileUtil.UploadFile(pdf.getName(),pdf_in);
							if(z.isNotNull(pdf_file_url)){
								String orderid = pdf.getName().substring(0,pdf.getName().lastIndexOf("."));
								z.getSqlSession().update("update","update s_invoice set invoice_file='"+pdf_file_url+"' where orderid='"+orderid+"'");
							}
						}
						result.setSuccess("上传成功");
					}else{
						result.setError("未获取到文件");
					}
				}else{
					result.setError("未获取到文件");
				}
			}
		} catch (Exception e) {
			result.setError("文件上传失败："+e.getMessage());
		}
		return result;
	}

}
